package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.system.domain.bo.SheetBo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.TReportVo;
import com.ruoyi.system.domain.bo.TReportBo;
import com.ruoyi.system.service.ITReportService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 上报
 *
 * @author ruoyi
 * @date 2024-03-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/report")
public class TReportController extends BaseController {

    private final ITReportService iTReportService;

    /**
     * 查询上报列表
     */
    @SaCheckPermission("system:report:list")
    @GetMapping("/list")
    public TableDataInfo<TReportVo> list(TReportBo bo, PageQuery pageQuery) {
        return iTReportService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出上报列表
     */
    @SaCheckPermission("system:report:export")
    @Log(title = "上报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TReportBo bo, HttpServletResponse response) {
        List<TReportVo> list = iTReportService.queryList(bo);
        ExcelUtil.exportExcel(list, "上报", TReportVo.class, response);
    }

    /**
     * 获取上报详细信息
     *
     * @param pkid 主键
     */
    @SaCheckPermission("system:report:query")
    @GetMapping("/{pkid}")
    public R<TReportVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String pkid) {
        return R.ok(iTReportService.queryById(pkid));
    }

    /**
     * 新增上报
     */
    @SaCheckPermission("system:report:add")
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TReportBo bo) {
        return toAjax(iTReportService.insertByBo(bo));
    }

    /**
     * 修改上报
     */
    @SaCheckPermission("system:report:edit")
    @Log(title = "上报", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TReportBo bo) {
        return toAjax(iTReportService.updateByBo(bo));
    }

    /**
     * 删除上报
     *
     * @param pkids 主键串
     */
    @SaCheckPermission("system:report:remove")
    @Log(title = "上报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pkids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] pkids) {
        return toAjax(iTReportService.deleteWithValidByIds(Arrays.asList(pkids), true));
    }
    /**
     * 保存模板数据
     */
    @PostMapping("/saveLuckySheet")
    public R<Void> saveLuckySheet(@RequestBody SheetBo bo) {
        return toAjax(iTReportService.saveLuckySheet(bo));
    }
}
